package com.tjy.Interface;


import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginInterface implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        //1.判断是否和登录相关
        String requestURI = request.getRequestURI();
        //和登录相关 放行
        if (requestURI.contains("login") || requestURI.contains("toLogin") ) {
            return true;
        }
        //2.判断是否在登录状态
        Object user = request.getSession().getAttribute("user");
        //在登录状态 放行
        if (null != user) {
            return true;
        } else {
            //不在登陆状态 拦截 跳转登录页面
            request.getRequestDispatcher("/html/login.jsp").forward(request, response);
            return false;
        }

    }
}
